﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.TextUtils;
using CatUtilsDll.Utils.Currency.TextUtils;
using CatUtilsDll.Utils.Currency.WebUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CatUtils.Utils.SpecialUtils.Html
{
    /// <summary>
    /// 爬虫通用帮助类
    /// </summary>
    public class SiteCurrencryHelper
    {
        /// <summary>
        /// 分割标题用的字符串，默认是一个制表符，如需要请自行修改。
        /// </summary>
        public static string TitleSplit { get; set; } = "   ";

        /// <summary>
        /// 文章信息
        /// </summary>
        public class ArticleInfo
        {
            /// <summary>
            /// 标题
            /// </summary>
            public string Title { get; set; } = "";

            /// <summary>
            /// 文本
            /// </summary>
            public string Content { get; set; } = "";

            /// <summary>
            /// 标签列表
            /// </summary>
            public List<string> TagList { get; set; } = new List<string>();

            /// <summary>
            /// 要发往的Wordpress 分类ID
            /// </summary>
            public List<int> ClassId { get; set; } = new List<int>();
        }

        /// <summary>
        /// 获取链接信息
        /// </summary>
        /// <param name="ArticleUrl">文章链接</param>
        /// <param name="TitleXpath">文章标题路径</param>
        /// <param name="ContentXpath">文章信息路径</param>
        /// <param name="TagXpath">文章标签Xpath</param>
        /// <returns></returns>
        public static ArticleInfo GetArticleInfo(string ArticleUrl, string TitleXpath, string ContentXpath, string TagXpath)
        {
            return GetArticleInfo(ArticleUrl, new List<string>() { TitleXpath }, new List<string> { ContentXpath }, new List<string> { TagXpath });
        }

        /// <summary>
        /// 获取链接信息
        /// </summary>
        /// <param name="ArticleUrl">文章链接</param>
        /// <param name="TitleXpath">文章标题路径</param>
        /// <param name="ContentXpath">文章信息路径</param>
        /// <param name="TagXpath">文章标签Xpath</param>
        /// <returns></returns>
        public static ArticleInfo GetArticleInfo(string ArticleUrl, List<string> TitleXpath, List<string> ContentXpath, List<string> TagXpath)
        {
            if (ArticleUrl == "")
                return new ArticleInfo();
            return GetArticleInfoWithHtml(WebHelper.HttpPcGet(ArticleUrl), TitleXpath, ContentXpath, TagXpath);
        }

        /// <summary>
        /// 获取链接信息
        /// </summary>
        /// <param name="ArticleHtml">文章源码</param>
        /// <param name="TitleXpath">文章标题路径</param>
        /// <param name="ContentXpath">文章信息路径</param>
        /// <param name="TagXpath">文章标签Xpath</param>
        /// <returns></returns>
        public static ArticleInfo GetArticleInfoWithHtml(string ArticleHtml, string TitleXpath, string ContentXpath, string TagXpath)
        {
            return GetArticleInfoWithHtml(ArticleHtml, new List<string>() { TitleXpath }, new List<string> { ContentXpath }, new List<string> { TagXpath });
        }

        /// <summary>
        /// 获取链接信息
        /// </summary>
        /// <param name="ArticleHtml">文章源码</param>
        /// <param name="TitleXpath">文章标题路径</param>
        /// <param name="ContentXpath">文章信息路径</param>
        /// <param name="TagXpath">文章标签Xpath</param>
        /// <returns></returns>
        public static ArticleInfo GetArticleInfoWithHtml(string ArticleHtml, List<string> TitleXpath, List<string> ContentXpath, List<string> TagXpath)
        {
            ArticleInfo article = new ArticleInfo();

            if (ArticleHtml == "")
            {
                Console.WriteLine("文章为空");
                return article;
            }
            foreach (var item in TitleXpath)
            {
                if (item == "")
                    continue;

                article.Title = $"{article.Title}{TitleSplit}{Transcoding.HtmlDecode(HtmlHelper.GetFirstText(ArticleHtml, item))}".Trim();
            }
            foreach (var item in ContentXpath)
            {
                if (item == "")
                    continue;
                article.Content = $"{article.Content}{HtmlHelper.GetFirstHtml(ArticleHtml, item)}";
            }
            foreach (var item in TagXpath)
            {
                if (item == "")
                    continue;
                article.TagList.AddRange(HtmlHelper.GetTextValueList(ArticleHtml, item));
            }
            if (article.TagList.Count > 0)
                article.TagList = ListHelper.ListRepeat(article.TagList);
            return article;
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="API"></param>
        /// <param name="Xpath"></param>
        /// <param name="StartPageText"></param>
        /// <param name="EndPageText"></param>
        /// <returns></returns>
        public static List<string> GetArticleList(string API, string Xpath, string StartPageText, string EndPageText)
        {
            return GetArticleList(API, Xpath, TextHelper.StringToInt(StartPageText), TextHelper.StringToInt(EndPageText));
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="API"></param>
        /// <param name="Xpath"></param>
        /// <param name="StartPage"></param>
        /// <param name="EndPage"></param>
        /// <returns></returns>
        public static List<string> GetArticleList(string API, string Xpath, int StartPage, int EndPage)
        {
            List<string> ArticleList = new List<string>();

            for (; StartPage <= EndPage; StartPage++)
            {
                ArticleList.AddRange(GetArticleList(string.Format(API, StartPage), Xpath));
            }
            return ArticleList;
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="API"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        public static List<string> GetArticleList(string API, string Xpath)
        {
            string Shtml = WebHelper.HttpPcGet(API);
            return GetArticleListWithHtml(Shtml, Xpath);
        }

        /// <summary>
        /// 获取文章列表  直接传入html
        /// </summary>
        /// <param name="Shtml"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        public static List<string> GetArticleListWithHtml(string Shtml, string Xpath)
        {
            if (Shtml == "" || Xpath == "")
                return new List<string>();

            return HtmlHelper.GetLinkVlaueList(Shtml, Xpath);
        }

        /// <summary>
        /// 给相对地址链接添加host，例如 /content/web.jpg 添加主域名：http://baidu.com  结果为 http://baidu.com/content/web.jpg
        /// </summary>
        /// <param name="ArticleList"></param>
        /// <param name="Host"></param>
        /// <returns></returns>
        public static List<string> ArticleListAddHost(List<string> ArticleList, string Host)
        {
            List<string> TempList = new List<string>();
            foreach (var item in ArticleList)
            {
                TempList.Add(Host + item);
            }
            return TempList;
        }
    }
}